#!/bin/sh

set -e

VERSION=5.2
BASEDIR="$PWD"
ROOTDIR="$BASEDIR/ROOTFS"
SRCDIR="$BASEDIR/osu-micro-benchmarks-$VERSION"

wget http://mvapich.cse.ohio-state.edu/download/mvapich/osu-micro-benchmarks-$VERSION.tar.gz -O osu-micro-benchmarks-$VERSION.tar.gz
tar -xzf osu-micro-benchmarks-$VERSION.tar.gz

cd "$SRCDIR"
# ./configure fails if CFLAGS includes -shared.
# Use wrapper to add -shared to gcc cmdline later at make stage.
cat <<EOF >"$SRCDIR/my-gcc"
#!/bin/bash
# export CFLAGS2='-fPIC -shared'
gcc \$CFLAGS2 "\$@"
EOF
chmod a+x my-gcc

# ./configure will try to run compiled binary, and as we compile it with -lmpi,
# we also have to run it with (correct) libmpi.so - the one from $HOME/openmpi-bin.
export LD_LIBRARY_PATH="$HOME/openmpi-bin/lib/:$LD_LIBRARY_PATH"

export CFLAGS2=''
# mike-apps/open-mpi is installed into $HOME/openmpi-bin/, reuse the same dir for OSU tests.
# Host binaries will be in $HOME/openmpi-bin/libexec/osu-micro-benchmarks/mpi/.
#
# for host
export CFLAGS_PREFIX="--prefix=$HOME/openmpi-bin/"
./configure CFLAGS="-I$HOME/openmpi-bin/include" LDFLAGS="-L$HOME/openmpi-bin/lib" LIBS="-lmpi -lopen-rte -lopen-pal" $CFLAGS_PREFIX CC="$SRCDIR/my-gcc"
# clean will remove binaries from OSv build
make clean
make -j`nproc` && make install
#
# for OSv VM
export CFLAGS_PREFIX="--prefix=$ROOTDIR"
./configure CFLAGS="-I$HOME/openmpi-bin/include" LDFLAGS="-L$HOME/openmpi-bin/lib" LIBS="-lmpi -lopen-rte -lopen-pal" $CFLAGS_PREFIX CC="$SRCDIR/my-gcc"
#
export CFLAGS2='-fPIC -shared'
# clean will remove binaries from host/Linux build
make clean
make -j`nproc`
make install
# file $ROOTDIR/libexec/osu-micro-benchmarks/mpi/pt2pt/osu_latency

cat <<EOF >$BASEDIR/usr.manifest
/**: $ROOTDIR/**
EOF

#
